#include "MainWindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QDesktopWidget>
#include <QPainter>
#include <QColorDialog>
#include <QColor>
#include <QBrush>


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->ui->mid_act->setChecked(true);    // 默认选中窗口大小为 "中"
    this->ui->init_act->setChecked(true);   // 默认选中窗口背景为初始
    this->setFixedSize(this->cell * 30, this->cell * 20);
}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    this->setFixedSize(this->cell * 30, this->cell * 20);
    // 窗口在屏幕居中显示
    QDesktopWidget *desk = QApplication::desktop();
    int wd = desk->width();
    int ht = desk->height();
    this->move((wd - width()) / 2, (ht - height()) / 2);

    QPainter pt(this);
    pt.setPen(Qt::NoPen);
    pt.setBrush(this->bgColor);
    pt.drawRect(this->rect());
}

/**
 * 清除窗口大小菜单选项勾选
 * @brief MainWindow::clrWinSizeAct
 */
void MainWindow::clrWinSizeAct()
{
    this->ui->big_act->setChecked(false);
    this->ui->mid_act->setChecked(false);
    this->ui->small_act->setChecked(false);
}

/**
 * 清除窗口背景色菜单项勾选
 * @brief MainWindow::clrBgAct
 */
void MainWindow::clrBgAct()
{
    this->ui->init_act->setChecked(false);
    this->ui->red_act->setChecked(false);
    this->ui->blue_act->setChecked(false);
    this->ui->yellow_act->setChecked(false);
    this->ui->custom_act->setChecked(false);
}


void MainWindow::on_red_act_triggered()
{
    this->bgColor = QColor(Qt::red);
    update();
    this->clrBgAct();
    this->ui->red_act->setChecked(true);
}

void MainWindow::on_blue_act_triggered()
{
    this->bgColor = Qt::blue;
    update();
    this->clrBgAct();
    this->ui->blue_act->setChecked(true);
}

void MainWindow::on_yellow_act_triggered()
{
    this->bgColor = Qt::yellow;
    update();
    this->clrBgAct();
    this->ui->yellow_act->setChecked(true);
}

void MainWindow::on_custom_act_triggered()
{

    qDebug() << "open edit color";
    QColorDialog dlg(this);
    dlg.setWindowTitle("自定义颜色");
    dlg.setCurrentColor(Qt::red);

    if (dlg.exec() == QColorDialog::Accepted) {
        QColor color = dlg.selectedColor();
        qDebug() << color;
        this->bgColor = color;
        this->clrBgAct();
        this->ui->custom_act->setChecked(true);
    }
}


void MainWindow::on_init_act_triggered()
{
    this->bgColor = Qt::transparent;
    update();
    this->clrBgAct();
    this->ui->init_act->setChecked(true);
}

void MainWindow::on_big_act_triggered()
{
    this->cell = this->maxSize * 0.8;
    update();
    this->clrWinSizeAct();
    ui->big_act->setChecked(true);
}

void MainWindow::on_mid_act_triggered()
{
    this->cell = this->maxSize * 0.6;
    update();
    this->clrWinSizeAct();
    ui->mid_act->setChecked(true);
}

void MainWindow::on_small_act_triggered()
{
    this->cell = this->maxSize * 0.4;
    update();
    this->clrWinSizeAct();
    ui->small_act->setChecked(true);
}




